﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.Serialization.Json;
using System.IO;

namespace EmailSender
{
    public partial class SenderSet : Form
    {
        public string SenderAddress
        {
            get { return txtSenderMailAddress.Text.Trim(); }
            set { txtSenderMailAddress.Text = value; }
        }
        public string SmtpHost
        {
            get { return txtSmtpServer.Text.Trim(); }
            set { txtSmtpServer.Text = value; }
        }
        public string UserName
        {
            get { return txtUserName.Text.Trim(); }
            set { txtUserName.Text = value; }
        }
        public string Password
        {
            get { return txtPassword.Text; }
            set { txtPassword.Text = value; }
        }
        public string MailTitle
        {
            get { return txtMailTitle.Text; }
            set { txtMailTitle.Text = value; }
        }
        public string MailContent
        {
            get { return rtxtMailContent.Text; }
            set { rtxtMailContent.Text = value; }
        }

        public string DisplayName
        {
            get { return txtDisplayName.Text; }
            set { txtDisplayName.Text = value; }
        }

        public SenderSet()
        {
            InitializeComponent();
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Model.EmailSet));
            string path = Application.StartupPath + "\\senderConfig.ini";
            if (File.Exists(path))
            {
                using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    Model.EmailSet emailSet = serializer.ReadObject(fs) as Model.EmailSet;
                    if (null != emailSet)
                    {
                        SenderAddress = emailSet.SenderAddress;
                        SmtpHost = emailSet.SmtpHost;
                        UserName = emailSet.UserName;
                        Password = emailSet.Password;
                        DisplayName = emailSet.DisplayName;
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SenderSet_FormClosing(object sender, FormClosingEventArgs e)
        {
            Model.EmailSet emailSet = null;
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Model.EmailSet));
            string path = Application.StartupPath + "\\senderConfig.ini";
            if (File.Exists(path))
            {
                using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    emailSet = serializer.ReadObject(fs) as Model.EmailSet;
                }
            }
            if (null == emailSet)
                emailSet = new Model.EmailSet();
            emailSet.Password = Password;
            emailSet.SenderAddress = SenderAddress;
            emailSet.SmtpHost = SmtpHost;
            emailSet.UserName = UserName;
            emailSet.DisplayName = DisplayName;
            using (FileStream fs = new FileStream(path, FileMode.Truncate))
            {
                serializer.WriteObject(fs, emailSet);
                fs.Flush();
                fs.Close();
            }

        }
    }
}
